public class LeetCode203 {
    public ListNode removeElements(ListNode head, int val){

        if (head == null){
            return null;
        }

        ListNode res = removeElements(head.next, val);

        if (head.val == val){
            return res;
        }else {
            head.next = res;
            return head;
        }
    }

    /**
     * 与上面的方法等价
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements2(ListNode head, int val) {
        if (head == null){
            return null;
        }

        head.next = removeElements2(head.next, val);

        return head.val == val ? head.next : head;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 6, 3, 4, 5, 6};
        ListNode head = new ListNode(nums);
        System.out.println(head);

        ListNode res = new LeetCode203().removeElements2(head, 6);
        System.out.println(res);
    }
}
